<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>伪类</title>
    <style>
      pseudo-classes-c::part(div-part) {
        color: #fff;
        background-color: #f498b2;
      }
      pseudo-classes-c::part(span-part) {
        color: #f498b2;
        background-color: #fce6ec;
      }
    </style>
  </head>
  <body>
    <h1>:host</h1>
    <pseudo-classes-a></pseudo-classes-a>
    <h1>:host([disabled])</h1>
    <pseudo-classes-a disabled></pseudo-classes-a>
    <h1>:host(.italic-theme)</h1>
    <pseudo-classes-a class="italic-theme"></pseudo-classes-a>
    <h1>:host-context(.dark-theme)</h1>
    <div class="dark-theme">
      <pseudo-classes-b></pseudo-classes-b>
    </div>
    <h1>::part()</h1>
    <pseudo-classes-c></pseudo-classes-c>
    <template id="template">
      <h2>Hello H2</h2>
      <p>Hello P</p>
      <div part="div-part">Hello Div</div>
      <span part="span-part">Hello Span</span>
    </template>
    <script>
      class PseudoClassesA extends HTMLElement {
        constructor() {
          super();
          const shadow = this.attachShadow({ mode: "open" });

          const templateDOM = document.getElementById("template");

          const cloneTemplate = templateDOM.content.cloneNode(true);

          const style = document.createElement("style");

          style.innerHTML = `
                    h2 {
                        color: green;
                    }
                    p {
                        color: blue;
                    }
                    span {
                        background-color: yellow;
                    }
                    :host {
                        color: #333;
                    }
                    :host([disabled]) {
                        opacity: 0.5;
                    }
                    :host(.italic-theme) {
                        font-style: italic;
                    }
                `;

          shadow.appendChild(style);
          shadow.appendChild(cloneTemplate);
        }
      }

      class PseudoClassesB extends HTMLElement {
        constructor() {
          super();
          const shadow = this.attachShadow({ mode: "open" });

          const templateDOM = document.getElementById("template");

          const cloneTemplate = templateDOM.content.cloneNode(true);

          const style = document.createElement("style");

          style.innerHTML = `
                    h2 {
                        color: green;
                    }
                    p {
                        color: blue;
                    }
                    :host-context(.dark-theme) {
                        background-color: #1a1a1a;
                        color: #999;
                    }
                `;

          shadow.appendChild(style);
          shadow.appendChild(cloneTemplate);
        }
      }

      class PseudoClassesC extends HTMLElement {
        constructor() {
          super();
          const shadow = this.attachShadow({ mode: "open" });

          const templateDOM = document.getElementById("template");

          const cloneTemplate = templateDOM.content.cloneNode(true);

          const style = document.createElement("style");

          style.innerHTML = `
                    h2 {
                        color: green;
                    }
                    p {
                        color: blue;
                    }
                    span {
                        background-color: yellow;
                    }
                `;

          shadow.appendChild(style);
          shadow.appendChild(cloneTemplate);
        }
      }

      customElements.define("pseudo-classes-a", PseudoClassesA);

      customElements.define("pseudo-classes-b", PseudoClassesB);

      customElements.define("pseudo-classes-c", PseudoClassesC);
    </script>

    <script>
      // 检查 :host-context() 支持
      const isHostContextSupported = CSS.supports(
        "selector(:host-context(.test))"
      );

      if (!isHostContextSupported) {
        console.warn("浏览器不支持 :host-context()");
      }
    </script>
  </body>
</html>
